import os
import numpy as np
import pandas as pd
import logging


train_dir = "/home/datanfs/macong_data/tencent_data/train_preliminary"
test_dir = "/home/datanfs/macong_data/tencent_data/test.zip"

ad_csvf = "ad.csv"
click_csvf = "click_log.csv"
user_csv = "user.csv"

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

# 合并click_log.csv和user.csv
# 简单构建时间序列的点击行为
def merge_click_uid():
    click_f = os.path.join(train_dir, click_csvf)
    uid_f = os.path.join(train_dir, user_csv)
    store_f = os.path.join(train_dir, "click_uid.csv")

    if os.path.exists(click_f) is False:
        logger.error("error: {} not exit".format(click_f))
        raise FileExistsError
    if os.path.exists(uid_f) is False:
        logger.error("error: {} not exit".format(uid_f))
        raise FileExistsError

    df_click = pd.read_csv(click_f)
    df_uid = pd.read_csv(uid_f)

    df_uid["gender"] = df_uid["gender"].apply(lambda x : x-1)
    df_uid["age"] = df_uid["age"].apply(lambda x : x-1)

    click_uid_merge = pd.merge(df_click, df_uid,on="user_id",how="left")
    df_m = click_uid_merge.groupby("user_id").apply(lambda x: x.sort_values('time', ascending=True))
    df_m = df_m.rename(columns={"user_id":"uid"})
    # df_m = df_m.groupby("user_id")["creative_id"].apply(list).reset_index()
    helper={"creative_id":list,
            "time":list,
            "click_times":list}
    df_m = df_m.groupby("user_id").agg(helper).reset_index()
    df_n = pd.merge(df_uid, df_m, on="user_id", how="left")

    df_n.to_csv(store_f, index=False)

# 合并click_log.csv和ad.csv
def merge_click_ad():
    click_f = os.path.join(train_dir, click_csvf)
    ad_f = os.path.join(train_dir, ad_csvf)

    if os.path.exists(click_f) is False:
        logger.error("error: {} not exit".format(click_f))
        raise FileExistsError
    if os.path.exists(ad_f) is False:
        logger.error("error: {} not exit".format(ad_f))
        raise FileExistsError

    store_f = os.path.join(train_dir, "click_uid.csv")

    df_click = pd.read_csv(click_f)
    df_uid = pd.read_csv(ad_f)

    click_uid_merge = pd.merge(df_click, df_uid, on="user_id", how="left")
    print(click_uid_merge.keys())

    click_uid_merge.to_csv(store_f, index=False)


if __name__ == '__main__':
    merge_click_uid()

